package com.myopen.controller.admin;

import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.jfinal.aop.Before;
import com.jfinal.core.ActionKey;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.myopen.base.BaseController;
import com.myopen.base.BaseDataTableVo;
import com.myopen.base.BaseVo;
import com.myopen.model.Website;
import com.myopen.validator.MyWebsiteValidator;

public class WebsiteController extends BaseController{

	public void index() {
		render("/admin/website/index.html");
	}
	
	@ActionKey("/admin/website/pagequery")
	public void pagequery(){
		
		Record record=getSearchRecord();//会自动组装通用的参数
		record.set("searchContent", getPara("searchContent"));
		record.set("status", getPara("status"));
		Page<Record> page=Website.dao.pagequery(record);
		
		BaseDataTableVo tableVo=new BaseDataTableVo(page);
		renderJson(tableVo);
	}
	
	@ActionKey("/admin/website/startUsing")
	public void startUsing(){
		
		Record currUser=getAdminUser();
		String id=getPara("id");
		Website.dao.changeStatus(id, "WEBSITE_STATUS_00", currUser);
		
		BaseVo baseVo=new BaseVo();
		renderJson(baseVo);
		
	}
	
	@ActionKey("/admin/website/forbidden")
	public void forbidden(){
		
		Record currUser=getAdminUser();
		String id=getPara("id");
		Website.dao.changeStatus(id, "WEBSITE_STATUS_01", currUser);
		
		BaseVo baseVo=new BaseVo();
		renderJson(baseVo);
		
	}
	
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@ActionKey("/admin/myWebsite")
	public void myWebsite(){
		
		Record currUser=getAdminUser();
		Record myWebsite=Website.dao.getMyWebsite(currUser.getStr("id"));
		
		String configJson=myWebsite.getStr("config");
		Map<String,Object> config=(Map)JSON.parse(configJson);
		if(config==null) {
			config=new HashMap<>();
		}
		Map<String,Object> smtp=(Map)config.get("smtp");
		if(smtp==null) {
			smtp=new HashMap<>();
			smtp.put("port", 25);
			smtp.put("way", "SSL");
		}
		
		
		setAttr("vo", myWebsite);
		setAttr("smtp", smtp);
		render("/admin/myWebsite/index.html");
		
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Before(MyWebsiteValidator.class)
	@ActionKey("/admin/myWebsite/save")
	public void myWebsiteSave(){
		
		BaseVo baseVo=new BaseVo();
		Record currUser=getAdminUser();
		
		Record myWebsite=Website.dao.getMyWebsite(currUser.getStr("id"));
		
		Website vo=new Website();
		vo.set("name", getPara("name"));
		vo.set("sld", getPara("sld"));
		vo.set("domain", getPara("domain"));
		vo.set("id", myWebsite.getStr("id"));
		
		String configJson=myWebsite.getStr("config");
		Map<String,Object> config=(Map)JSON.parse(configJson);
		if(config==null) {
			config=new HashMap<>();
		}
		
		Map<String,Object> smtp=(Map)config.get("smtp");
		if(smtp==null) {
			smtp=new HashMap<>();
		}
		smtp.put("name", getPara("smtp.name"));
		smtp.put("email", getPara("smtp.email"));
		smtp.put("password", getPara("smtp.password"));
		smtp.put("host", getPara("smtp.host"));
		smtp.put("port", getPara("smtp.port"));
		smtp.put("way", getPara("smtp.way"));
		//TODO 可以验证下，发件箱是否正确
		config.put("smtp", smtp);
		
		vo.set("config", JSON.toJSONString(config));
		
		//进行一些必要的验证,这个验证放在了MyWebsiteValidator里面
		vo=Website.dao.save(vo,currUser);
		
		baseVo.setCode("0");
		
		renderJson(baseVo);
	}
	
}
